package arithmetic

import (
	"codejy/calculate"
	"codejy/globalfunc"
	"strconv"
)

type MinusArithmeticCalculate struct {
	name          string                  //计算器名称
	priority      int                     //优先级
	calculateData calculate.CalculateData //计算器数据
	result        string                  //计算结果

}

func init() {
	//注册计算器
	calculate.RegisterCalculate(&MinusArithmeticCalculate{
		name:     "-",
		priority: 2,
	})
}

func (a *MinusArithmeticCalculate) GetName() string {
	return a.name
}

func (a *MinusArithmeticCalculate) IsBool(symbol string) bool {
	if symbol == a.name {
		// 判断是否是数字
		b1 := globalfunc.ExecuteFunc("IsNumber", a.calculateData.A).(bool)
		b2 := globalfunc.ExecuteFunc("IsNumber", a.calculateData.B).(bool)
		if b1 && b2 {
			return true
		}
		panic("减法运算必须是数字: " + a.calculateData.A + "-" + a.calculateData.B)

	}
	return false
}

func (a *MinusArithmeticCalculate) AddExpression(a1, symbol, b1 string) {
	a.calculateData = calculate.CalculateData{
		A:      a1,
		Symbol: symbol,
		B:      b1,
	}
}

func (a *MinusArithmeticCalculate) Calculate() {
	//判断是否是小数
	b3 := globalfunc.ExecuteFunc("IsFloat", a.calculateData.A).(bool)
	b4 := globalfunc.ExecuteFunc("IsFloat", a.calculateData.B).(bool)
	if b3 || b4 {
		A1, _ := strconv.ParseFloat(a.calculateData.A, 64)
		B1, _ := strconv.ParseFloat(a.calculateData.B, 64)
		a.result = strconv.FormatFloat(A1-B1, 'f', -1, 64)
	} else {
		A1, _ := strconv.Atoi(a.calculateData.A)
		B1, _ := strconv.Atoi(a.calculateData.B)
		a.result = strconv.Itoa(A1 - B1)
	}
}

func (a *MinusArithmeticCalculate) GetResult() string {
	return a.result
}

func (a *MinusArithmeticCalculate) GetPriority() int {
	return a.priority
}
